package com.zx.练习题._2022面试题;

import com.zx._12_算法.ListNode;
import java.util.Arrays;

/**
 * 约瑟夫环:给你一个环(123456789),要求每三个数移除一个(124578->1257->127->1),返回最后一个数?
 */
public class Demo3 {

    public static void main1(String[] args) {
        ListNode begin = ListNode.getNodes(1, 2, 3, 4, 5, 6, 7, 8, 9);

        ListNode first = begin.get(0);
        ListNode last = begin.get(8);
        last.next = first;

        ListNode curr = begin;
        while (curr.val != curr.next.val) {

            ListNode a1 = curr;
            ListNode a2 = curr.next;
            ListNode a3 = curr.next.next;
            ListNode a4 = curr.next.next.next;

            a2.next = a4;
            a3.next = null;
            curr = a4;
        }

        System.out.println("最后一个是：" + curr.val);
    }

    public static void main(String[] args) {
        int[] nums = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        int counter = nums.length;
        int gap = 3;
        int begin = 0;
        while (counter > 1) {
            for (int i = 0; i < nums.length; i++) {
                if (nums[i] == -1) {
                    continue;
                } else {
                    begin++;
                    if (begin == gap) {
                        nums[i] = -1;
                        counter--;
                        begin = 0;
                    }
                }
            }
            print(nums);
        }


    }

    private static void print(int[] nums) {
        long[] longs = Arrays.stream(nums).asLongStream().filter(x -> x != -1).toArray();
        System.out.println("移除数据后：" + Arrays.toString(longs));
    }

}
